package com.example.demo.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.Service.IAddressService;
import com.example.demo.entity.Address;
import com.example.demo.entity.LoginUser;
import com.example.demo.entity.MsUser;
import com.example.demo.entity.params.AddressCreateParam;
import com.example.demo.entity.params.AddressUpdateParam;
import com.example.demo.entity.vo.AddressVo;
import com.example.demo.entity.vo.UserInfoVo;
import com.example.demo.mapper.AddressMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.mapping.AddressMapping;
import com.example.demo.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements IAddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<AddressVo> select(HttpServletRequest request) {

        Long userId = JWTUtil.getUserId(request.getHeader("Authorization"));
        List<Address> addresses = addressMapper.selectList(new LambdaQueryWrapper<Address>().eq(Address::getUserId,userId).eq(Address::getDelFlag,0L));

        List<AddressVo> list = new ArrayList<>();
        for(Address address :addresses){

            AddressVo addressVo = new AddressVo();
            addressVo.setAddressId(address.getAddressId());
            addressVo.setPhone(address.getPhone());
            addressVo.setArea(address.getArea());
            addressVo.setAddressDtl(address.getAddressDtl());
            addressVo.setAddressTags(address.getAddressTags());
            addressVo.setUserId(address.getUserId());
            addressVo.setName(address.getName());

            list.add(addressVo);
        }

        return list;
    }

    @Override
    public int create(HttpServletRequest request, AddressCreateParam addressCreateParam) {

        Long userId = JWTUtil.getUserId(request.getHeader("Authorization"));

        Address address = new Address();

        address.setPhone(addressCreateParam.getPhone());
        address.setArea(addressCreateParam.getArea());
        address.setAddressDtl(addressCreateParam.getAddressDtl());
        address.setAddressTags(addressCreateParam.getAddressTags());
        address.setUserId(userId);
        address.setName(addressCreateParam.getName());

        return addressMapper.insert(address);
    }

    @Override
    public int update(AddressUpdateParam addressUpdateParam) {

        Address address = new Address();

        address.setPhone(addressUpdateParam.getPhone());
        address.setArea(addressUpdateParam.getArea());
        address.setAddressDtl(addressUpdateParam.getAddressDtl());
        address.setAddressTags(addressUpdateParam.getAddressTags());
        address.setAddressId(addressUpdateParam.getAddressId());
        address.setName(addressUpdateParam.getName());

        return addressMapper.update(address,new LambdaQueryWrapper<Address>().eq(Address::getAddressId,address.getAddressId()));

    }

    @Override
    public int delete(Long addressId) {

        Address address = new Address();
        address.setDelFlag(1l);

        return addressMapper.update(address,new LambdaQueryWrapper<Address>().eq(Address::getAddressId,addressId));
    }


}
